let app = getApp();
const moment = require('../../utils/moment.min.js')
const util = require('../../utils/dateUtils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: null,
    roomList: [],
    tabActive: 0,
    tabsItem: [{
      name: '全部',
      status: '',
    },
    {
      name: '正在直播',
      status: 0,
    },
    {
      name: '直播预告',
      status: 1,
    },
    {
      name: '历史直播',
      status: 2,
    }
    ],
    liveStatus: 0,
    loadingMoreHidden: true,
    curPage: 1,
    pagesize: 10,
    totalPages: 1,
    hideHeader: true,
    hideBottom: true,
    tabCartTotal:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //监听app全局变量变化回调
  watchBack: function (value){
    this.setData({
      tabCartTotal: value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //加载
  onReachBottom: function () {
    let that = this;
    that.setData({
      curPage: that.data.curPage + 1,
      hideBottom: false
    });
    if (that.data.curPage < that.data.totalPages || that.data.curPage == that.data.totalPages) {
      this.getLiveList()
    }
    setTimeout(function () {
      that.setData({
        hideBottom: true
      })
    }, 1000);
  },
  //刷新
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      curPage: 1
    });
    that.setData({
      hideHeader: false,
      loadingMoreHidden: true,
    })
    setTimeout(function () {
      var date = new Date();
      that.setData({
        currentPage: 1,
        hideHeader: true
      })
      that.getLiveList()
    }, 1000);
    wx.stopPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    _this.setData({
      user: app.getUser(),
    })
    _this.setData({
      tabCartTotal: wx.getStorageSync('cartTotal')
    });
    app.watch(this.watchBack);//监听app全局变量
    if (_this.data.user.id == null) return;
    _this.getLiveList();
  },
  getLiveList: function () {
    let that = this;
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/zhibo/roomList',
      data: {
        centreId: app.globalData.loginCentreId,
        liveStatus: that.data.liveStatus,
        page: that.data.curPage,
        size: that.data.pagesize,
      },
      load: 0
    }).then((res) => {
      // for (let key in res.data.data) {
      //   if (res.data.data[key].start_time)
      //     var startTime = '';
      //   startTime = res.data.data[key]['start_time'];
      //   res.data.data[key].start_time = util.formatTimeThree(startTime, "Y-M-D h:m:s");
      //   if (res.data.data[key].end_time)
      //     res.data.data[key].end_time = util.formatTimeThree(res.data.data[key]['end_time'], "Y-M-D h:m:s");
      // };
      // that.setData({
      //   roomList: res.data.data,
      // });
      var tempArray = that.data.roomList;
      var totalpage = Math.ceil(parseInt(res.data.data.total) / that.data.pagesize);
      for (let key in res.data.data.rows) {
        if (res.data.data.rows[key].start_time)
          var startTime = '';
        startTime = res.data.data.rows[key]['start_time'];
        res.data.data.rows[key].start_time = util.formatTimeThree(startTime, "Y-M-D h:m:s");
        if (res.data.data.rows[key].end_time)
          res.data.data.rows[key].end_time = util.formatTimeThree(res.data.data.rows[key]['end_time'], "Y-M-D h:m:s");
      };
      that.setData({
        'roomList': res.data.data.rows,
        'totalPages': totalpage,
      });
      let roomList = [];
      if (that.data.curPage == 1) {
        that.setData({
          'roomList': res.data.data.rows,
        })
      } else {
        tempArray = tempArray.concat(res.data.data.rows);
        that.setData({
          loadingMoreHidden: true,
          roomList: tempArray,
        });
      };
      tempArray = [];
      if (res.data.data.rows.length != that.data.pagesize) {
        that.setData({
          loadingMoreHidden: false,
        });
      }
    })
  },
  tabChange: function (event) {
    console.log(event);
    let that = this;
    if (event.detail.index == 0) {
      that.setData({
        liveStatus: '',
        curPage: 1,
      })
    } else if (event.detail.index == 1) {
      that.setData({
        liveStatus: '101',
        curPage: 1,
      })
    } else if (event.detail.index == 2) {
      that.setData({
        liveStatus: '102',
        curPage: 1,
      })
    } else if (event.detail.index == 3) {
      that.setData({
        liveStatus: '103',
        curPage: 1,
      })
    };
    that.getLiveList();
  }
})